<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/12
 * Time: 11:01
 */

namespace app\modules\setting\controllers\cpgl;


use app\common\enum\QlUserTiktokProductGoods_banEnumText;
use app\common\enum\QlUserTiktokProductGoods_typeEnum;
use app\common\helper\CommonHelper;
use app\models\EtcProductBan;
use app\modules\setting\configs\cpgl\CplbConfig;
use app\modules\setting\configs\cpgl\GncplbConfig;
use app\modules\setting\models\cpgl\QlUserTiktokProductGoodsSearch;
use sookie\core\controllers\AdminController;
use sookie\excel\SaveExcel;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use Yii;

class GncplbController extends AdminController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new QlUserTiktokProductGoodsSearch();
            $dataProvider = $searchModel->searchData(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $item = $item->toArray();
                    $item['type'] = QlUserTiktokProductGoods_typeEnum::getMsg($item['type']);
                    $ban = intval(EtcProductBan::find()->andWhere(['id'=>$item['id']])->exists());
                    $item['ban'] = QlUserTiktokProductGoods_banEnumText::getMsg($ban);
                    $item['name'] = CommonHelper::getProductName($item);
                    return $item;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],GncplbConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }


    public function actionOn($id){
        try{
            $model = $this->findModel($id);
            $ban = EtcProductBan::find()->andWhere(['id'=>$model->id])->one();
            if($ban instanceof EtcProductBan){
                $ban->delete();
            }
            return ["ok" => true,"msg" => "撤销成功"];
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    public function actionDown($id){
        try{
            $model = $this->findModel($id);
            $ban = EtcProductBan::find()->andWhere(['id'=>$model->id])->one();
            if(!$ban instanceof EtcProductBan){
                $ban = new EtcProductBan();
                $ban->id = $model->id;
                $ban->save();
            }
            return ["ok" => true,"msg" => "下架成功"];
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }






    protected function findModel($id)
    {
        if (($model = QlUserTiktokProductGoods::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }



    /**
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * 导出
     */
    public function actionExport(){
        try {
            $tableConfig = CplbConfig::getExcelHeaderMap();
            $searchModel = new QlUserTiktokProductGoodsSearch();
            $params = ArrayHelper::merge(Yii::$app->request->get(),["pageSize" => 0,"page" => 1]);
            $dataProvider = $searchModel->searchData($params,array_values($tableConfig["config"]));
            $dataArr = ArrayHelper::toArray($dataProvider->getModels());

            if (!$dataArr){
                throw new Exception("没有可导出的数据");
            }
            $handler = new SaveExcel([
                'fileName' => $tableConfig["fileName"],
                'format' => $tableConfig["format"],
                "array" => $dataArr,
                'headerDataArray' => array_keys($tableConfig["config"]),
            ]);

            $handler->arrayToExcel();exit;
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }
}